package org.apmap.base;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import java.util.HashMap;
import java.util.Map;

public class Main {
    private static final SessionFactory ourSessionFactory;

    static {
        try {
            ourSessionFactory = new AnnotationConfiguration().
                    configure("hibernate.cfg.xml").
                    buildSessionFactory();
        }
        catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static Session getSession() throws HibernateException {
        return ourSessionFactory.openSession();
    }

    public static void main(final String[] args) throws Exception {
        final Session session = getSession();
        try {
            User user = new User();
            user.setLogin("aaa");
            user.setPassword("bbb");
            Map<String, String> map = new HashMap<String, String>();
            map.put("a", "111");
            map.put("b", "222");
            user.setProfile(map);

            session.beginTransaction();
            session.save(user);
            session.getTransaction().commit();
            session.flush();

        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            //transaction.rollback();
            session.close();
        }
    }
}
